package com.renyisima.wujiansima.demo.test;

import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;

public class MyRunnable implements Runnable {
//    表示源文件
    public File oldFile;
//    表示目标文件
    public File newFile;

    public MyRunnable(String oldFile, String newFile) {
        this.oldFile = new File(oldFile);
        this.newFile = new File(newFile);
    }

    @Override

    public void run() {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldFile));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
            byte[] b = new byte[1024];
            int len ;
            long length = oldFile.length();
            double i = 0;
            DecimalFormat df = new DecimalFormat("##.##%");
            while ((len = bis.read(b)) != -1){
                bos.write(len);
                i = i + len;
                double d = i / length;
                String s = df.format(d);
                bos.flush();
                System.out.println(Thread.currentThread().getName() + oldFile.getName() + "文件复制到" + newFile.getName() + "文件进度为：" + s);
            }
            System.out.println("复制完成");
            bis.close();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
